#include "Sock.hpp"

void Usage(std::string proc)
{
    std::cerr << "Usage: " << proc << " port" << std::endl;
}

void* HandlerRequest(void*arg)
{
    pthread_detach(pthread_self());
    int sockfd = *(int*)arg;
    
    std::string Http_response;
    Http_response += "HTTP/1.1 200 OK";
    Http_response += "Hello world!\n";
    Http_response += "Content-Length:";
    write(sockfd, Http_response.c_str(), Http_response.size());
    close(sockfd);
    
}

int main(int argc, char* argv[])
{
    if(argc != 2){
        Usage(argv[0]);
    }

    int listen_sockfd = Sock::Socket();
    Sock::Bind(listen_sockfd, atoi(argv[1]));
    Sock::Listen(listen_sockfd);

    while(true){
        int sockfd = Sock::Accept(listen_sockfd);
        pthread_t tid;
        pthread_create(&tid, nullptr, HandlerRequest, (void*)&sockfd);
    }
    return 0;
}